//
//  TabBarView.m
//  Mtime2.0
//
//  Created by 凉气的Mac on 16/9/3.
//  Copyright © 2016年 凉气的Mac. All rights reserved.
//

#import "TabBarView.h"

#import <Masonry.h>

@interface TabBarView ()

@property(nonatomic,weak) UIButton *selectBtn;

@end

@implementation TabBarView


-(void)addBtnWithNormal:(UIImage *)normalImage andSecect:(UIImage *)selectImage{

    UIButton *button = [[UIButton alloc]init];
    
    [button setImage:normalImage forState:UIControlStateNormal];
    
    [button setImage:selectImage forState:UIControlStateSelected];
    
    [button addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:button];
    
}


-(void)layoutSubviews{

    NSInteger subCount = self.subviews.count;
    
    CGFloat btnW = self.width / subCount;
    
    for (int i = 0; i < subCount; i ++) {
        
        CGFloat btnX = i * btnW;
        
        UIButton *tempBtn = self.subviews[i];
        
        tempBtn.tag = i;
        
        [tempBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.bottom.equalTo(self);
            
            make.width.equalTo(@(btnW));
            
            make.left.equalTo(self).offset(btnX);
            
            if (i == 0) {
                
                [self clickBtn:tempBtn];
            }
            
        }];
        
    }

}


#pragma mark --
#pragma mark --按钮点击
-(void)clickBtn:(UIButton *)button{

    self.selectBtn.selected = NO;
    
    button.selected = YES;
    
    self.selectBtn = button;
    
    if ([self.delegata respondsToSelector:@selector(tabBarView:changeControllerWithIndex:)]) {
        
        [self.delegata tabBarView:self changeControllerWithIndex:button.tag];
    }
    

}

@end










